package com.magicrule.prj.common.tree;

import java.util.ArrayList;
import java.util.List;

import com.magicrule.car.systemManage.model.Resource;

public class TreeBuilder {

	/**
	 * 
	 * @Title: bulid   
	 * @Description: TODO(对资源List建立树形结构)   
	 * @param: @param Resources
	 * @param: @return      
	 * @return: List<Resource>      
	 * @throws
	 */
	public static List<Resource> build(List<Resource> resourceList) {
		 
        List<Resource> trees = new ArrayList<Resource>();
        
        for (Resource resource : resourceList) {
        	resource.setText(resource.getResourceName());
        	/**
        	 * 只有parentId=0的放在集合trees中，也就是作为第一级
        	 */
            if (0==resource.getParentId()) {
                trees.add(resource);
            }
 
            for (Resource it : resourceList) {
                if (it.getParentId() == resource.getId()) {
                    if (resource.getChildren() == null) {
                        resource.setChildren(new ArrayList<Resource>());
                    }
                    resource.getChildren().add(it);
                }
            }
        }
        return trees;
    }
}
